package com.hdyg.sixg.activity.home;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.allen.library.SuperTextView;
import com.hdyg.sixg.R;
import com.hdyg.sixg.activity.common.BaseActivity;
import com.hdyg.sixg.entry.ExchangeBean;
import com.hdyg.sixg.mvp.Contrant.CExchange;
import com.hdyg.sixg.mvp.base.BasePresenter;
import com.hdyg.sixg.mvp.base.BaseUrl;
import com.hdyg.sixg.mvp.base.GetParamUtil;
import com.hdyg.sixg.mvp.presenter.PExchange;
import com.hdyg.sixg.util.StringUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import butterknife.BindView;

/**
 * @author CZB
 * @describe 转账界面
 * @time 2019/4/4 17:17
 */
public class ExchangeActivity extends BaseActivity implements CExchange.IVExchange {

    @BindView(R.id.ll_top_left)
    LinearLayout llTopLeft;
    @BindView(R.id.tvTopTitle)
    TextView tvTopTitle;
    @BindView(R.id.sv_balance)
    SuperTextView svBalance;
    @BindView(R.id.sv_pay_address)
    SuperTextView svPayAddress;
    @BindView(R.id.et_collect_address)
    EditText etCollectAddress;
    @BindView(R.id.iv_sao)
    ImageView ivSao;
    @BindView(R.id.et_exchange_number)
    EditText etExchangeNumber;
    @BindView(R.id.sv_fee)
    SuperTextView svFee;
    @BindView(R.id.et_pay_pwd)
    EditText etPayPwd;
    @BindView(R.id.tv_fee_des)
    TextView tvFeeDes;
    @BindView(R.id.bt_sure)
    Button btSure;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;

    private CExchange.IPExchange mPresenter;
    private ExchangeBean dataBean;
    private String walletaddress, amount, trapass;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_exchange;
    }

    @Override
    protected void initView() {
        onClickListener();
        tvTopTitle.setText(R.string.exchange_title);
        ivSao.setVisibility(View.GONE);
    }

    private void onClickListener() {
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            srlRefresh.finishRefresh(2000);//传入false表示刷新失败
            initData();
        });
        llTopLeft.setOnClickListener(v -> finish());
        etExchangeNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                amount = s.toString();
                //手续费 = number*free/100
                if (!TextUtils.isEmpty(amount)) {
                    double str1 = Double.valueOf(amount) * Double.valueOf(dataBean.getRate()) / 100;
                    svFee.setRightString(StringUtil.dealFloatStr(str1));
                }
            }
        });

        btSure.setOnClickListener(v -> {

            walletaddress = etCollectAddress.getText().toString().trim();
            amount = etExchangeNumber.getText().toString().trim();
            trapass = etPayPwd.getText().toString().trim();
            if (TextUtils.isEmpty(walletaddress)) {
                toastNotifyShort(R.string.exchange_collect_address_hint);
                return;
            }
            if (TextUtils.isEmpty(amount)) {
                toastNotifyShort(R.string.exchange_number_hint);
                return;
            }
            if (TextUtils.isEmpty(trapass)) {
                toastNotifyShort(R.string.pay_pwd_hint);
                return;
            }
            if (StringUtil.isFastDoubleClick()){
                return;
            }
            mPresenter.pGetApplyExchange(BaseUrl.START_TRANSFER_URL, GetParamUtil.getApplyTransParam(walletaddress, amount, trapass));
        });
    }

    @Override
    protected void initData() {
        mPresenter.pGetInit(BaseUrl.TRANSFER_URL, GetParamUtil.getTransferParam());
    }

    @Override
    protected void createPresenter() {
        mPresenter = new PExchange(this);
    }

    @Override
    public void vGetInitSuccess(ExchangeBean dataBean) {
        //number*free/100
        this.dataBean = dataBean;
        svBalance.setRightString(dataBean.getAmount());
        svPayAddress.setLeftBottomString(dataBean.getWallet_token());
        String str = String.format(getResources().getString(R.string.exchange_fee_des),dataBean.getRate());
        tvFeeDes.setText(str);
    }

    @Override
    public void vGetApplyExchangeSuccess(String dataBean) {
        toastNotifyShort(dataBean);
        setResult(RESULT_OK);
        finish();
    }
}
